<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/12
 * Time: 11:30
 */
namespace App\Service\Api;

use App\Http\Model\Mysql\UserAddressModel;
use Carbon\Carbon;

class AddressService extends BaseService
{
    /**
     * 用户地址列表
     * @return array
     */
    public function addressList()
    {
        $user_info  =   request()->post('api_user');
        $user_id    =   $user_info['user_id'];

        $list   =   UserAddressModel::where(['user_id' => $user_id, 'is_delete' => UserAddressModel::NOT_DELETE])->select(['id', 'user_name', 'province', 'city', 'area', 'address', 'tel_number', 'postal_code', 'national_code', 'is_default'])->get();
        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'list'  =>  $list->toArray()
            ]];
        }
    }

    /**
     * 用户添加地址
     * @param array $params
     * @return array
     */
    public function addressAppend(array $params)
    {
        $user_info  =   request()->post('api_user');
        $user_id    =   $user_info['user_id'];

        $insert['user_id']  =   $user_id;
        $insert['user_name']=   $params['userName'];
        $insert['province'] =   $params['provinceName'];
        $insert['city']     =   $params['cityName'];
        $insert['area']     =   $params['countyName'];
        $insert['address']  =   $params['detailInfo'];
        $insert['tel_number']   =   $params['telNumber'];
        $insert['postal_code']  =   $params['postalCode'] ?? '';
        $insert['national_code']=   $params['nationalCode'] ?? '';
        $insert['is_default']   =   $params['is_default'] ?? 0;

        if ($insert['is_default'] == 1) {
            //更新其他地址为非默认地址
            UserAddressModel::where(['user_id' => $user_id])->update(['is_default' => 0]);
        }
        $flag   =   UserAddressModel::create($insert);
        if ($flag) {
            return [true, '添加成功'];
        }
        return [false, '添加失败'];
    }

    /**
     * 用户删除地址
     * @param int $address_id  地址ID
     * @return array
     */
    public function addressDelete(int $address_id)
    {
        $user_info  =   request()->post('api_user');
        $user_id    =   $user_info['user_id'];

        $del_flag   =   UserAddressModel::where(['id' => $address_id, 'user_id' => $user_id])->update(['is_delete' => UserAddressModel::IS_DELETE, 'delete_time' => Carbon::now()->timestamp]);
        if ($del_flag) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 用户编辑地址
     * @param int $address_id  地址ID
     * @param array $params
     * @return array
     */
    public function addressEdit(int $address_id, array $params)
    {
        $update['user_name']=   $params['userName'];
        $update['province'] =   $params['provinceName'];
        $update['city']     =   $params['cityName'];
        $update['area']     =   $params['countyName'];
        $update['address']  =   $params['detailInfo'];
        $update['tel_number']   =   $params['telNumber'];
        $update['postal_code']  =   $params['postalCode'] ?? '';
        $update['national_code']=   $params['nationalCode'] ?? '';
        $update['is_default']   =   $params['is_default'] ?? 0;

        if ($update['is_default'] == 1) {
            //更新其他地址为非默认地址
            UserAddressModel::where(['id' => $address_id])->update(['is_default' => 0]);
        }

        $flag   =   UserAddressModel::where(['id' => $address_id, 'is_delete' => UserAddressModel::NOT_DELETE])->update($update);
        if ($flag) {
            return [true, '编辑成功'];
        }

        return [false, '编辑失败'];
    }

}
